﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using WarehouseManagement.Application.Command.WarehouseTypeCommand;
using WarehouseManagement.Domain.DTO;
using WarehouseManagement.Domain.DTO.WarehouseType;

namespace WarehouseManagement.Controllers
{
    /// <summary>
    /// 仓库分类
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class WareTypeController : ControllerBase
    {

        private readonly IMediator _mediator;


        public WareTypeController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 仓库分类添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddWareType(AddWareCategoryDto command)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.AddName = name;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                command.User_Id = int.Parse(userId);
                result.Data = await _mediator.Send(new CreateWareTypeCommand { AddWareTypeDTO = command});
              
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }


            return result;
        }


        /// <summary>
        /// 树形显示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<WareTypeSelectDTO>>> GetWareTypeSelectList([FromQuery]SelectWareTypeCommand command)
        {
            ApiResult<List<WareTypeSelectDTO>> result = new ApiResult<List<WareTypeSelectDTO>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
             return result;
        }


        /// <summary>
        /// 仓库分类显示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<SelWareTypeDTO>>> GetWareType([FromQuery]ShowWareTypeCommand command)
        {

            ApiResult<List<SelWareTypeDTO>> result = new ApiResult<List<SelWareTypeDTO>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;

        }



        /// <summary>
        /// 仓库分类删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> DelWarehouseType(DelWareTypeCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.DelName = name;

                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }


        /// <summary>
        /// 反填仓库分类
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<UptWareHouseTypeDto>> FillWarehouseType([FromQuery]FillWareTypeCommand command)
        {
            ApiResult<UptWareHouseTypeDto> result = new ApiResult<UptWareHouseTypeDto>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }


        /// <summary>
        /// 仓库分类修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UptWareType(UptWareHouseTypeDto command)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.UpdName = name;
                result.Data = await _mediator.Send(new UptwareTypeCommand { uptDto = command });

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }


            return result;
        }

    }
}
